package com.sinosoft.system.bank.dao.imp;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.sinosoft.system.bank.dao.BankPersonalDao;
import com.sinosoft.system.bank.entity.BankPersonal;

   /**
    * @ClassName BankPersonalDaoJdbc实现类
    * @date 2017-2-16 
    * @author shangxp
    */  
@Repository
public class BankPersonalDaoJdbc implements BankPersonalDao {
	private static Logger log = Logger.getLogger(BankPersonalDaoJdbc.class);
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public int insertBankPersonal(BankPersonal bankPersonal) {
		String sqlStr = "insert into bank_personal ( "
							+" id ,"	
							+" sys_user_id ,"	
							+" personame ,"	
							+" assets ,"	
							+" assetsrmb ,"	
							+"is_del) values( "
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" ?,"
							 +" 0 )";
		log.debug(sqlStr+"\n"+bankPersonal.toString());
		try{
			Object[] args = {
					bankPersonal.getId(),
					bankPersonal.getSysUserId(),
					bankPersonal.getPersoname(),
					bankPersonal.getAssets(),
					bankPersonal.getAssetsrmb()
			};
			return jdbcTemplate.update(sqlStr, args);
		}catch(Exception e){
			log.error("数据添加异常 SQL："+sqlStr+"\n"+bankPersonal.toString()+e.getMessage());
		}
		return 0;
	}

	@Override
	public int updateBankPersonal(BankPersonal bankPersonal) {
		String sqlStr = "update bank_personal set "
									+" id = ? , "
									+" sys_user_id = ? , "
									+" personame = ? , "
									+" assets = ? , "
									+" assetsrmb = ?  "
									+" where id = ?";
		log.debug(sqlStr+"\n"+bankPersonal.toString());
		try{
			Object[] args = {
					bankPersonal.getId(),
					bankPersonal.getSysUserId(),
					bankPersonal.getPersoname(),
					bankPersonal.getAssets(),
					bankPersonal.getAssetsrmb(),
					bankPersonal.getId()
			};
			return jdbcTemplate.update(sqlStr, args);
		}catch(Exception e){
			log.error("数据更新异常 SQL："+sqlStr+"\n"+bankPersonal.toString()+e.getMessage());
		}
		return 0;
	}

	@Override
	public int deleteBankPersonal(int id) {
		String sqlStr = "update bank_personal set is_del = 1  where id = ?";
		try{
			Object[] args = {
					id
			};
			return jdbcTemplate.update(sqlStr, args);
		}catch(Exception e){
			log.error("数据删除异常"+sqlStr+"\nID="+id+"\n"+e.getMessage());
		}	return 0;
	}

	@Override
	public BankPersonal selectBankPersonal(int id) {
		String sqlStr = "select * from bank_personal where is_del =0 and id = ?";
		log.debug(sqlStr+"\nID="+id);
		try{
			Object[] args = {
				id
			};
			return jdbcTemplate.queryForObject(sqlStr, args, new BankPersonalMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr+"\nID="+id+"\n"+e.getMessage());
		}	return null;
	}

	@Override
	public BankPersonal selectBankPersonal(List<String> queryKeys,List<Object> queryValues) {
		StringBuffer sqlStr = new StringBuffer("select * from bank_personal where is_del =0 " );
		Object[] args = null;
		if(queryKeys!=null && !queryKeys.isEmpty()){	
			args = new Object[queryKeys.size()];
			for(int i=0;i<queryKeys.size();i++){
				sqlStr.append(" and "+queryKeys.get(i)+"=? ");
				args[i] = queryValues.get(i);
			}
		}
		log.debug(sqlStr.toString());
		try{
			return jdbcTemplate.queryForObject(sqlStr.toString(), args, new BankPersonalMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public int selectCountBankPersonal() {
		String sqlStr = "select count(1) from bank_personal where is_del =0 ";
		log.debug(sqlStr);
		try{
			return jdbcTemplate.queryForObject(sqlStr, Integer.class);
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr+"\n"+e.getMessage());
		}
		return 0;
	}

	@Override
	public List<BankPersonal> selectLimitBankPersonal(int start, int size) {
		String sqlStr = "select ";
					sqlStr+=" id ,";
					sqlStr+=" sys_user_id ,";
					sqlStr+=" personame ,";
					sqlStr+=" assets ,";
					sqlStr+=" assetsrmb ";
			sqlStr+=" from bank_personal where is_del =0  order by id desc limit ?,?";
		log.debug(sqlStr);
		try{
		Object[] args = {
				start,
				size
		};
			return jdbcTemplate.query(sqlStr, args, new BankPersonalMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public List<BankPersonal> selectAllBankPersonal() {
		String sqlStr = "select ";
					sqlStr+=" id ,";
					sqlStr+=" sys_user_id ,";
					sqlStr+=" personame ,";
					sqlStr+=" assets ,";
					sqlStr+=" assetsrmb ";
		 sqlStr+=" from bank_personal where is_del =0 order by id desc ";
		log.debug(sqlStr);
		try{
			return jdbcTemplate.query(sqlStr, new BankPersonalMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public int selectCountBankPersonal(List<String> queryKeys,List<Object> queryValues) {
		StringBuffer sqlStr = new StringBuffer("select count(1) from bank_personal where is_del = 0 ");
		Object[] args = null;
		if(queryKeys!=null && !queryKeys.isEmpty()){	
			args = new Object[queryKeys.size()];
			for(int i=0;i<queryKeys.size();i++){
				sqlStr.append(" and "+queryKeys.get(i)+" = ? ");
				args[i] = queryValues.get(i) ;
			}
		}		
		log.debug(sqlStr.toString());
		try{
			return jdbcTemplate.queryForObject(sqlStr.toString(), args,Integer.class);
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		return 0;
	}

	@Override
	public List<BankPersonal> selectLimitBankPersonal(List<String> queryKeys,List<Object> queryValues,int start,int size) {
		StringBuffer sqlStr = new StringBuffer("select ");
					sqlStr.append(" id ,");
					sqlStr.append(" sys_user_id ,");
					sqlStr.append(" personame ,");
					sqlStr.append(" assets ,");
					sqlStr.append(" assetsrmb ");
			sqlStr.append(" from bank_personal where is_del =0 " );
		Object[] args = null;
			if(queryKeys!=null && !queryKeys.isEmpty()){	
				args = new Object[queryKeys.size()+2];
				for(int i=0;i<queryKeys.size();i++){
					sqlStr.append(" and "+queryKeys.get(i)+" = ? ");
					args[i] =  queryValues.get(i) ;
				}
			}
		if(args==null){
			args = new Object[]{start,size};
		}else{
			args[queryKeys.size()] = start;
			args[queryKeys.size()+1] = size;
		}
		sqlStr.append(" order by id desc ");
		sqlStr.append(" limit ?,?");
		log.debug(sqlStr.toString());
		try{
			return jdbcTemplate.query(sqlStr.toString(), args, new BankPersonalMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		return null;
	}

	@Override
	public List<BankPersonal> selectAllBankPersonal(List<String> queryKeys,List<Object> queryValues) {
		StringBuffer sqlStr = new StringBuffer("select ");
					sqlStr.append(" id ,");
					sqlStr.append(" sys_user_id ,");
					sqlStr.append(" personame ,");
					sqlStr.append(" assets ,");
					sqlStr.append(" assetsrmb ");
			sqlStr.append("from bank_personal where is_del =0 " );
		Object[] args = null;
			if(queryKeys!=null && !queryKeys.isEmpty()){	
				args = new Object[queryKeys.size()];
				for(int i=0;i<queryKeys.size();i++){
					sqlStr.append(" and "+queryKeys.get(i)+" = ? ");
					args[i] = queryValues.get(i);
				}
			}
		sqlStr.append(" order by id desc ");
		log.debug(sqlStr.toString());
		try{
			return jdbcTemplate.query(sqlStr.toString(), args, new BankPersonalMapper());
		}catch(Exception e){
			log.error("数据查询异常"+sqlStr.toString()+"\n"+e.getMessage());
		}
		return null;
	}

	protected class BankPersonalMapper implements RowMapper<BankPersonal> {
		@Override
		public BankPersonal mapRow(ResultSet arg0, int arg1) throws SQLException {
			BankPersonal bankPersonal = new BankPersonal();
			if(arg0!=null){
					bankPersonal.setId(arg0.getInt("id"));
					bankPersonal.setSysUserId(arg0.getInt("sys_user_id"));
					bankPersonal.setPersoname(arg0.getString("personame"));
					bankPersonal.setAssets(arg0.getString("assets"));
					bankPersonal.setAssetsrmb(arg0.getString("assetsrmb"));
				}
			return bankPersonal;
		}  
	}
}

